local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local EventList = ServerOHOH.load.EventList();
local Spin_Cfg=ServerOHOH.config.Spin;
local ItemTool=_G.CommonOHOH.load.ItemTool();
function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl());
	self.dataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
    self.data = self.dataBase:GetData("spinReward");

	self.parent.RobuxBuyDoneEvent:Connect(function(gid,gtype)
		if gtype=="turntable" then
			local cfg=_G.CommonOHOH.config.Goods[gid]
			local count=0 
			for k,v in pairs(cfg.itemData) do
				count=count+v
			end
			self:BuySpin(count)
        end
	end)
end;
function M.dtor(self)
	M.super.dtor(self);
end;
--购买旋转次数
function M.BuySpin(self,count)
	self.data.spinTimes=self.data.spinTimes+count;
	self:SubmitSpinData();
	return true;
end
--转盘
function M.StartSpin(self)
	if(self.data.spinTimes<=0)then
		return;
	end
	local rewardIndex= Spin_Cfg.RewardSetting[self.data.rewardSettingStep];
	local reward=Spin_Cfg.Reward[rewardIndex];
	local key,count= ItemTool.GetRewardByLevel(reward.item_id,reward.count,self.dataBase:GetData("base").level);
	self.ItemCtrl:AddAnyThing(key,count);

	
	self.data.spinTimes=self.data.spinTimes-1;

	self.data.rewardSettingStep=self.data.rewardSettingStep+1;
	if(self.data.rewardSettingStep>#Spin_Cfg.RewardSetting)then
		self.data.rewardSettingStep=1;
	end
	self:SubmitSpinData();
end;
--增加免费次数
function M.AddFreeTimes(self)
	--验证
	local startTime=self.data.lastSpinTime;
	local endTime=os.time();
	local needIntervalTime=Spin_Cfg.IntervalTime[self.data.intervalTimeStep];--需要间隔的时间  
	local intervalTime= os.difftime(endTime, startTime);
	if(intervalTime>=needIntervalTime)then
		self.data.spinTimes=self.data.spinTimes+1;
		self.data.lastSpinTime=os.time();
		self.data.intervalTimeStep = self.data.intervalTimeStep+1;
		if(self.data.intervalTimeStep>#Spin_Cfg.IntervalTime)then
			self.data.intervalTimeStep=1;
		end
	end
	

	self:SubmitSpinData();
end;
function M.SubmitSpinData(self)
	self.dataBase:Execut(function()
	end);
	self.parent:SynchDataToClient("spinReward");
end
M:RegistEvent(EventList.spin_BuyTime,M.BuySpinTimes)
M:RegistEvent(EventList.spin_AddFreeTimes,M.AddFreeTimes)
M:RegistEvent(EventList.start_spin,M.StartSpin)
return M;